package com.shop.loan.util;


import cn.hutool.core.util.RandomUtil;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.rentaldata.RentalDataUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Component
public class SysMsgUtil {

    @Value("${sys.codeValidTime}")
    private Integer codeValidTime;

    @Value("${sys.platformName}")
    private String platformName;

    @Value("${sys.msg.today}")
    private String msgToday;

    @Value("${sys.msg.tom}")
    private String msgTom;

    @Value("${sys.msg.overdue}")
    private String msgOverdue;

    @Value("${sys.msg.auth_refuse}")
    private String auth_refuse;

    @Value("${sys.msg.auth_success}")
    private String auth_success;

    @Value("${sys.msg.loan_success}")
    private String loan_success;

    @Resource
    RedisUtil redisUtil;

    @Lazy
    @Resource
    RentalDataUtil rentalDataUtil;

    /**
     * 校验短信验证码
     *
     * @param phone
     * @param phoneCode
     * @return
     */
    public boolean checkVerifyCode(String phone, String phoneCode) {
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneCode)) {
            return false;
        }
        if ("112233".trim().equals(phoneCode)) {
//        if (".112233.".trim().equals(phoneCode) && phone.trim().startsWith("186498")) {
            return true;
        }
        String codeRedisKey = RedisCache.phoneCode + phone;
        String redisCode = redisUtil.get(codeRedisKey);
        if (phoneCode.trim().equals(redisCode)) {
            redisUtil.del(codeRedisKey);
            return true;
        }
        return false;
    }


    /**
     * 发送短信验证码
     *
     * @param phone
     * @return
     */
    public boolean sendVerifyCode(String phone) {
        String phoneCode = RandomUtil.randomNumbers(4);
        String content = "您的验证码为：" + phoneCode + "，该验证码5分钟内有效，请勿泄露于他人！";
//        String content = "《" + platformName + "》您的验证码为：" + phoneCode + "，该验证码5分钟内有效，请勿泄露于他人！";
        boolean isSuccess = rentalDataUtil.sendByContent(phone, content);
        if (isSuccess) {
            redisUtil.setex(RedisCache.phoneCode + phone, codeValidTime == null ? 60 * 5 : codeValidTime, phoneCode);//短信5分钟时效
        }
        return isSuccess;
    }

    /**
     * 发送登录成功短信验证码
     *
     * @return
     */
    public boolean sendLoginSuccessCodeByAdmin(String name, String mobile) {
        String content = "账号[" + name + "]登录成功，若不是本人操作，请及时处理。";
//        String content = "《" + platformName + "》账号[" + name + "]登录成功，若不是本人操作，请及时处理。";
        boolean isSuccess = rentalDataUtil.sendByContent(mobile, content);
        return isSuccess;
    }

    /**
     * 到账后短信提醒客户
     *
     * @return
     */
    public boolean sendLoanSuccess(String mobile) {
        String content = loan_success;
//        String content = "《" + platformName + "》" + loan_success;
        return rentalDataUtil.sendByContent(mobile, content);
    }


    /**
     * 审核通过后短信提醒客户
     *
     * @return
     */
    public boolean sendAuthSuccess(String mobile) {
        String content =  auth_success;
//        String content = "《" + platformName + "》" + auth_success;
        return rentalDataUtil.sendByContent(mobile, content);
    }

    /**
     * 审核拒绝短信通知用户
     *
     * @param mobile
     * @return
     */
    public boolean sendAuthFail(String mobile) {
        String content = auth_refuse;
//        String content = "《" + platformName + "》" + auth_refuse;
        return rentalDataUtil.sendByContent(mobile, content);
    }


    /**
     * 今日到期短信
     *
     * @param mobile
     * @return
     */
    public boolean sendToday(String mobile) {
//        String content = "《" + platformName + "》" + msgToday;
//        return rentalDataUtil.sendByContent(mobile, content);
        return false;
    }


    /**
     * 明日到期短信
     *
     * @param mobile
     * @return
     */
    public boolean sendTom(String mobile) {
//        String content = "《" + platformName + "》" + msgTom;
//        return rentalDataUtil.sendByContent(mobile, content);
        return false;
    }

    /**
     * 逾期短信
     *
     * @param mobile
     * @return
     */
    public boolean sendOverdue(String mobile) {
//        String content = "《" + platformName + "》" + msgOverdue;
//        return rentalDataUtil.sendByContent(mobile, content);
        return false;
    }

}
